package com.jin290.mqlibrary;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.aigestudio.wheelpicker.WheelPicker;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SheelPickerPopupWindow extends PopupWindow implements WheelPicker.OnItemSelectedListener {


    private View mMenuView;
    private TextView tv_alert_cancel;
    private TextView tv_alert_ok;
    private TextView tv_alert_delete;
    private WheelPicker main_wheel_hh;
    private WheelPicker main_wheel_mm;
    private WheelPicker main_wheel_mm2;
    private String hh = "6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22";
    private String mm = "00,05,10,15,20,25,30,35,40,45,50,55";
    private String mm2 = "05,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120";
    private List<String> mListhh;
    private List<String> mListmm;
    private List<String> mListmm2;

    private TextView Settextview;
    private String text = "6:00~6:05";
    private String shh = "06";
    private String smm = "00";
    private String smm2 = "05";

    /**
     * @param context
     */
    public SheelPickerPopupWindow(Activity context, TextView textView, OnClickListener itemsSheelPickerOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context
                .LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.alert_sheelpicker, null);
        Settextview = textView;
        tv_alert_ok = (TextView) mMenuView.findViewById(R.id.tv_alert_ok);
        tv_alert_delete = (TextView) mMenuView.findViewById(R.id.tv_alert_delete);
        tv_alert_cancel = (TextView) mMenuView.findViewById(R.id.tv_alert_cancel);
        main_wheel_hh = (WheelPicker) mMenuView.findViewById(R.id.main_wheel_hh);
        main_wheel_mm = (WheelPicker) mMenuView.findViewById(R.id.main_wheel_mm);
        main_wheel_mm2 = (WheelPicker) mMenuView.findViewById(R.id.main_wheel_mm2);
        main_wheel_hh.setOnItemSelectedListener(this);
        main_wheel_mm.setOnItemSelectedListener(this);
        main_wheel_mm2.setOnItemSelectedListener(this);


        mListhh = SB2(hh);
        mListmm = SB2(mm);
        mListmm2 =SB2(mm2);
        main_wheel_hh.setData(mListhh);
        main_wheel_mm.setData(mListmm);
        main_wheel_mm2.setData(mListmm2);

        //取消按钮
        tv_alert_cancel.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
//        //设置按钮监听
        tv_alert_ok.setOnClickListener(itemsSheelPickerOnClick);
        tv_alert_delete.setOnClickListener(itemsSheelPickerOnClick);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.popup_bottom_anim);
        //实例化一个ColorDrawapopup_bottom_ani
        ColorDrawable dw = new ColorDrawable(0x000000ff);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }


    @Override
    public void onItemSelected(WheelPicker picker, Object data, int position) {
        int i = picker.getId();
        if (i == R.id.main_wheel_hh) {
            shh = data.toString();

        } else if (i == R.id.main_wheel_mm) {
            smm = data.toString();

        } else if (i == R.id.main_wheel_mm2) {
            smm2 = data.toString();

        }
    }

    public void gettext() throws ParseException {
        text = shh + ":" + smm;

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        Date date = sdf.parse(text);
        long time = (Integer.parseInt(smm2) * 60 * 1000);
        Date afterDate = new Date(date.getTime() + time);//30分钟后的时间
        Settextview.setText(text + "~" + sdf.format(afterDate));
    }
    public void Deletetext()
    {
        Settextview.setText("--:--");
    }



    public static List<String> SB2(String s) {
        List<String> Slist = new ArrayList<>();
        String[] ary = s.split(",");//调用API方法按照逗号分隔字符串

        for (String ss : ary) {
            Slist.add(ss);
        }

        return Slist;
    }
}
